// MegaPorn.js for PSPTube
// Made By JK108
// Decrypt Function By Emre Korkmaz

function MegaPorn_CheckURL( url, option )
{
	if (url.match("http://www.megaporn.com") == "http://www.megaporn.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a MegaPorn URL
*/
function MegaPorn_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var un = contents.match(/flashvars.un \= \"(.*?)\"/);
	var un = un[1];
	var k1 = contents.match(/flashvars.k1 \= \"(.*?)\"/);
	var k1 = k1[1];
	var k2 = contents.match(/flashvars.k2 \= \"(.*?)\"/);
	var k2 = k2[1];
	var s = contents.match(/flashvars.s \= \"(.*?)\"/);
	var s = s[1];
	return "http://www" + s + ".megaporn.com/files/" + Decrypt(un, k1, k2) + "/";
}

function MegaPorn_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.megaporn.com/video/?c=search&s=" + keyword.replace(/ /g, "+") );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.megaporn.com/video/?c=search&s=" + keyword.replace(/ /g, "+") + "&p=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("</b> - did not match any videos. <br><br>") == "</b> - did not match any videos. <br><br>"){ result.total = 0; return result; }
	var total = contents.match(/font-weight\:bold; font-size\:12px;\"\>Videos (.*?)\/div\>/);
	var total = total[1].match(/ of (.*?)\</);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<TD width=\"150\" height=\"\" valign=\"top\">");

	if(pos != -1){
		contents = contents.substring(pos + 39, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = "http://www.megaporn.com/video/" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<IMG src\=\"(.*?)\"/);
		var thumb_url = ""; //thumb_url[1];
		//Title
		var title = contents.match(/text-decoration\:none;\"\>(.*?)\<\/a\>/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/color\:#F21F77;  font-size\:12px;\"\>(.*?)\<\/font\>/);
		var length = TimestringToSeconds(length[1]);
		//Description
		pos = contents.indexOf("id=\"description_long_");
		var description = contents.substring(pos + 21, contents.length);
		pos = description.indexOf("\n");
		description = description.substring(pos + 4, description.length);
		pos = description.indexOf("</div>");
		var pos2 = description.indexOf("(<a href=\"");
		if(pos2 < pos && pos2 != -1){
			description = splitTo2Lines(ImproveText(description.substring(0, pos2 - 1)), 60);
		} else{
			description = splitTo2Lines(ImproveText(description.substring(0, pos - 3)), 60);
		}
		//Views
		var views = contents.match(/Views\:\<\/font\> \<font class\=\"v5\"\>(.*?)\<\/font\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/Comments\:\<\/font\> \<font class\=\"v5\"\>(.*?)\<\/font\>/);
		var comments = parseInt(comments[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var MegaPorn = new Object();
MegaPorn.Name          = "MegaPorn";		/* Service name */
MegaPorn.Description   = "MegaPorn";		/* Service description */
MegaPorn.SearchDesc    = "MegaPorn";		/* Service search screen description */
MegaPorn.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
MegaPorn.CheckURL      = MegaPorn_CheckURL;	/* CheckURL function */
MegaPorn.GetURL        = MegaPorn_GetURL;	/* GetURL function */
MegaPorn.Search        = MegaPorn_Search;	/* Search function */

// add MegaPorn to site list
SiteList.push( MegaPorn );
